﻿using System;
using System.Windows.Forms;

namespace FindClone
{
    internal static class Utility
    {
        #region MessageType enum

        public enum MessageType
        {
            Main,
            SubMain,
            SubSubMain
        }

        #endregion

        public static void WriteToLog(this TextBox tb, string msg, MessageType messageType)
        {
            var quotes = GetQuotes(messageType);

            tb.Text += String.Format("{0} {1} {2} {3}{4}", DateTime.Now, quotes.Prefix, msg, quotes.Postfix,
                                     Environment.NewLine);
        }

        public static Quotes GetQuotes(MessageType messageType)
        {
            switch (messageType)
            {
                default:
                    return new Quotes {Prefix = "<<<", Postfix = ">>>"};

                case MessageType.Main:
                    return new Quotes {Prefix = "<<<", Postfix = ">>>"};

                case MessageType.SubMain:
                    return new Quotes {Prefix = "<<", Postfix = ">>"};

                case MessageType.SubSubMain:
                    return new Quotes {Prefix = "<", Postfix = ">"};
            }
        }

        public static string ToFileSize(this long source)
        {
            const int byteConversion = 1024;
            double bytes = Convert.ToDouble(source);
            if (bytes >= Math.Pow(byteConversion, 3)) //GB Range    
            {
                return string.Concat(Math.Round(bytes/Math.Pow(byteConversion, 3), 2), " GB");
            }
            if (bytes >= Math.Pow(byteConversion, 2)) //MB Range 
            {
                return string.Concat(Math.Round(bytes/Math.Pow(byteConversion, 2), 2), " MB");
            }
            if (bytes >= byteConversion) //KB Range   
            {
                return string.Concat(Math.Round(bytes/byteConversion, 2), " KB");
            }
            return string.Concat(bytes, " Bytes");
        }

        #region Nested type: Quotes

        public class Quotes
        {
            public string Prefix { get; set; }
            public string Postfix { get; set; }
        }

        #endregion
    }
}